var label;
var target;
var checkMark = "fa-check"; //set the font-awesome icon class
var iconSize = 'fa-2x'; //set iconSize

function formInit() {
    var validateFrom = $('#materialForm');
    validateFrom.validate({
        rules: {
            name: {
                required: true,
                'isName': true,
                rangelength: [2, 5]


            },
            tel: {
                required: true,
                'isMobile': true,
            },
            email: {
                required: true,
                email: true,
            },
            addr: 'required',
            id_card: {
                required: true,
                'isID': true
            },
            money:{
                number:true,
                required:true
            }

        },
        messages: {
            name: {
                required: '请输入姓名',
                'isName': '名字只能是汉字',
                rangelength: '名字长度不正确'

            },
            tel: {
                'required': "请输入联系电话",
                'isMobile': "电话格式不正确"
            },
            addr: '请输入地址',
            email: {
                required: '请输入email地址',
                email: '请输入正确的email地址'
            },
            id_card: {
                required: '请输入身份证号码',
                'isID': '请输入正确身份证号码',
            },
            money:{
                number:'必须为数字',
                required:'请输入金额'
            }

        }
    });

    jQuery.validator.addMethod("isMobile", function (value, element) {
        var reg = /^1[3578]\d{9}$/;
        return this.optional(element) || (reg.test(value));
    }, "手机格式不正确");

    jQuery.validator.addMethod("isName", function (value, element) {
        //var reg = /(.+)?\d(.+)?/;
        //return this.optional(element) || (!reg.test(value));
        var reg = /^[\u2E80-\u9FFF]+$/;
        return this.optional(element) || (reg.test(value));
    }, "姓名只能是汉字");
    jQuery.validator.addMethod("isID", function (value, element) {
        var reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/;
        return this.optional(element) || (reg.test(value));
    }, "身份证格式不正确");


}


$(function () {

    formInit();
    //表单效果
    $('.formInput').focusin(function () {
        $(this).prev().addClass('activeY');
    }).focusout(function () {
        if ($(this).val().trim() == '') {
            $(this).prev().removeClass('activeY');
        }
    });
    //icheck
    $('.year').iCheck({
        radioClass: 'iradio_square-blue',
    });
    //上传
    Dropzone.autoDiscover = false;
    $('#dropzone').dropzone({
        url: '/loans/upload',
        maxFiles: 3,
        maxFilesize: 2,
        paramName: 'file',
        acceptedFiles: ".png,.jpg,.jpeg",
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        addRemoveLinks: true,
        dictMaxFilesExceeded: '最多只能上传3个文件',
        dictFileTooBig: '上传的文件大小为：2MB',
        dictRemoveFile: '删除文件',
        dictCancelUpload: '取消上传',
        dictResponseError: '上传文件时出错',
        dictInvalidFileType: '不允许此类型的文件',
        dictDefaultMessage: '<i class="upload-icon ace-icon fa fa-cloud-upload fa-3x"></i><br>最多可添加3个附件，每个大小不超过2MB',
        init: function () {
            this.on('success', function (file, data) {
                file.code = data.code;
                if (data.code == '1000') {
                    file.file_id = data.id;
                    var html = '<input type="hidden" name="file_id[]" id="file-' + data.id + '" value="' + data.path + '"/>';
                    $('#file_update').append(html);
                } else {
                    this.removeFile(file);
                    console.log(data.msg);
                }
            });
            this.on('removedfile', function (file) {
                if (file.code == '1000') {
                    $.ajax({
                        type: 'post',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        url: '/loans/remove_file/' + file.file_id,
                        dataType: 'json',
                        success: function (json) {
                            if (json.code == '1000') {
                                $('#file-' + file.file_id).remove();
                            } else {
                                console.log(json.msg);
                            }
                        }
                    });
                }
            });
            this.on("maxfilesexceeded", function (file) {
                this.removeFile(file);
                console.log('最多只能添加3个文件');
            });
        }
    });
//    money_type

    if($('#id').val()==1){
        $('#money').attr('max','100000')
    }else if($('#id').val()==2){

        $('#money').attr({
            'min':'100000',
            'max':'500000'
        })
    }else if($('#id').val()==3){

        $('#money').attr({
            'min':'500000',
            'max':'1000000'

        })
    }


});